package com.bishe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bishe.config.EcoBootException;
import com.bishe.pojo.FileInfo;
import com.bishe.pojo.User;
import com.bishe.pojo.query.FileQuery;
import com.bishe.service.IFileService;
import com.bishe.service.IUserService;
import com.bishe.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * 文件控制器
 * @date 2022/2/16 16:59
 */
@RestController
@CrossOrigin
@RequestMapping("/file")
@Api(tags = "文件控制器")
public class FileController {

    @Resource
    private IFileService fileService;

    @Resource
    private IUserService userService;

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result upload(MultipartFile file,String dataId,String type,String key){
        if(StrUtil.isEmpty(type)){
            return Result.error("类型为空！");
        }
        FileInfo fileInfo = fileService.upload(file, dataId, type,key);
        return Result.ok("上传成功!",fileInfo);
    }

    @ApiOperation("获取密钥")
    @GetMapping("/getKey")
    public Result getKey(String type){
        if(StrUtil.isEmpty(type)){
            return Result.error("类型为空！");
        }
        switch (type.toLowerCase()){
            case "zuc": return Result.ok(ZUCUtil.getKey());
            case "sm2":return Result.ok(Sm2Util.getKey());
            case "sm4":return Result.ok("ok",Sm4Util.getKey());
        }
        return Result.error("错误类型！");
    }

    @ApiOperation("下载文件")
    @GetMapping("/download")
    public Result download(String fileId, HttpServletResponse response,String type,String key){
        FileInfo fileInfo = fileService.getById(fileId);
        if (fileInfo == null){
             throw new EcoBootException("文件为空！");
        }
        File file = ProjectUtils.getFile(fileInfo);

        if (type == null){
            return Result.error("类型为空！");
        }
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            //下载文件
            ProjectUtils.download(response,fileInputStream,file.getName(),type,key);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.ok();
    }


    @ApiOperation("删除文件")
    @DeleteMapping("/deleteFile/{fileId}")
    public Result deleteFile(@PathVariable String fileId){
       fileService.deleteFile(fileId);
        return Result.ok();
    }


    @ApiOperation("查询")
    @GetMapping("/toPage")
    public Result list(FileQuery query){
        //检查登录
        IPage<FileInfo> iPage = fileService.toPage(query);
        List<FileInfo> records = iPage.getRecords();
        if (CollUtil.isNotEmpty(records)){
            for (FileInfo record : records) {
                String createBy = record.getCreateBy();
                User byId = userService.getById(createBy);
                if (byId != null){
                    record.setCreateByName(byId.getRealName());
                }
            }
        }
        return Result.ok(iPage);
    }

}
